<template>
	<view class="">
		<view class="commentStyle ntop">新消息提醒</view>
		<view class="switchBox commentStyle">
			<view class="switchView">
				<text>接收消息通知</text>
				<u-switch space="2" v-model="detail.news" asyncChange activeColor="#FF5900" inactiveColor="#E5E5E5" @change="changeNews"></u-switch>
			</view>
		</view>
		<view class="commentStyle ntop">互动通知</view>

		<view class="switchBox commentStyle">
			<view class="switchView">
				<text>赞</text>
				<u-switch space="2" v-model="detail.praise" activeColor="#FF5900" inactiveColor="#E5E5E5"></u-switch>
			</view>
			<u-line color="#E6E6E6"></u-line>
			<view class="switchView">
				<text>新增粉丝</text>
				<u-switch space="2" v-model="detail.newFan" activeColor="#FF5900" inactiveColor="#E5E5E5"></u-switch>
			</view>
			<u-line color="#E6E6E6"></u-line>

			<view class="switchView">
				<text>评论</text>
				<u-switch space="2" v-model="detail.comment" activeColor="#FF5900" inactiveColor="#E5E5E5"></u-switch>
			</view>
			<u-line color="#E6E6E6"></u-line>

			<view class="switchView">
				<text>私信</text>
				<u-switch space="2" v-model="detail.private" activeColor="#FF5900" inactiveColor="#E5E5E5"></u-switch>
			</view>
		</view>
		<!-- 提示弹窗 -->
		<BaseToast :show="showClose" :title="title" :content="content" @cancel="cancel" @submit="submit"></BaseToast>
	</view>
</template>

<script>
import { mapState } from 'vuex';
import pageMixin from '@/mixins/pageMixin';

export default {
	mixins: [pageMixin],
	components: {},
	data() {
		return {
			detail: {
				news: true,
				praise: true,
				newFan: true,
				comment: true,
				private: true
			},
			showClose: false,
			content: '您还没有开启通知权限，无法接受到消息通知，是否前往设置？',
			title: '通知权限开启提醒'
		};
	},
	computed: {},

	watch: {
		'detail.news': {
			handler(newVal, oldVal) {
				console.log('通知：1111111111111111', newVal);
				if (newVal) {
					// 接受消息通知
					this.detail.praise = true;
					this.detail.newFan = true;
					this.detail.comment = true;
					this.detail.private = true;
					// 接收通知
				} else {
					this.detail.praise = false;
					this.detail.newFan = false;
					this.detail.comment = false;
					this.detail.private = false;
					// 接收关闭通知
				}
			},
			immediate: true,
			deep: true
		}
	},
	onLoad(options) {
		// this.judge();
	this.getQuanxian();
	},
	methods: {

		changeNews(e) {
			// 去判断权限
			this.getQuanxian(e,'change');
		},
		submit() {
			//跳转去手机权限设置
			this.openTongZhi()
		},
		cancel() {
			console.log('取消,代表没有权限');
			this.showClose = false;
			this.detail.news = false;
		},
		getQuanxian(e,type) {
			let platform = uni.getSystemInfoSync().platform; //首先判断app是安卓还是ios
			console.log(platform);
			if (platform == 'ios') {
				//这里是ios的方法
				console.log('我是iOS');
				var UIApplication = plus.ios.import('UIApplication');
				var app = UIApplication.sharedApplication();
				var enabledTypes = 0;
				if (app.currentUserNotificationSettings) {
					var settings = app.currentUserNotificationSettings();
					enabledTypes = settings.plusGetAttribute('types');
					console.log('enabledTypes1:' + enabledTypes);
					if (enabledTypes == 0) {
						//如果enabledTypes = 0 就是通知权限没有开启

						// 没有权限
						this.showClose = true;
					}else{
						if(type){
							// 有权限
							if(e){ // 接收通知

							}else{ // 关闭通知

							}
							this.detail.news = e;
						}
					}
				}
				plus.ios.deleteObject(settings);
			} else if (platform == 'android') {
				//下面是安卓的方法
				console.log('我是安卓', plus.android);
				var main = plus.android.runtimeMainActivity();
				var pkName = main.getPackageName();
				var uid = main.getApplicationInfo().plusGetAttribute('uid');
				var NotificationManagerCompat = plus.android.importClass('android.support.v4.app.NotificationManagerCompat');
				//android.support.v4升级为androidx
				if (NotificationManagerCompat == null) {
					NotificationManagerCompat = plus.android.importClass('androidx.core.app.NotificationManagerCompat');
				}
				var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
				// 未开通‘允许通知’权限，则弹窗提醒开通，并点击确认后，跳转到系统设置页面进行设置
				console.log(139, '安卓');
				if (!areNotificationsEnabled) {
					console.log(141, '安卓');
					// 没有权限
					this.showClose = true;
				}else{
						console.log(145, '安卓有权限');
					if(type){
						// 有权限
						if(e){ // 接收通知

						}else{ // 关闭通知

						}
						this.detail.news = e;
					}

				}
			}
		},
		//开启通知
		openTongZhi() {
			//弹窗按钮绑定方法
			let platform = uni.getSystemInfoSync().platform; //获取安卓还是ios
			this.tongzhi = false;
			if (platform == 'ios') {
				//如果机型是ios，ios由于权限问题，可能需要手动开启
				var UIApplication = plus.ios.import('UIApplication');
				var app = UIApplication.sharedApplication();
				var settings = app.currentUserNotificationSettings();
				enabledTypes = settings.plusGetAttribute('types');
				var NSURL2 = plus.ios.import('NSURL');
				var setting2 = NSURL2.URLWithString('app-settings:');
				var application2 = UIApplication.sharedApplication();
				application2.openURL(setting2);
				plus.ios.deleteObject(setting2);
				plus.ios.deleteObject(NSURL2);
				plus.ios.deleteObject(application2);
				plus.ios.deleteObject(settings);
			} else if (platform == 'android') {
				//如果机型是安卓
				var main = plus.android.runtimeMainActivity();
				var pkName = main.getPackageName();
				var uid = main.getApplicationInfo().plusGetAttribute('uid');
				var Intent = plus.android.importClass('android.content.Intent');
				var Build = plus.android.importClass('android.os.Build');
				//android 8.0引导
				if (Build.VERSION.SDK_INT >= 26) {
					//判断安卓系统版本
					var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
					intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
				} else if (Build.VERSION.SDK_INT >= 21) {
					//判断安卓系统版本
					//android 5.0-7.0
					var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
					intent.putExtra('app_package', pkName);
					intent.putExtra('app_uid', uid);
				} else {
					//(<21)其他--跳转到该应用管理的详情页
					intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
					var uri = Uri.fromParts('package', mainActivity.getPackageName(), null);
					intent.setData(uri);
				}
				// 跳转到该应用的系统通知设置页
				main.startActivity(intent);
			}
		},

	}
};
</script>

<style lang="scss" scoped>
.commentStyle {
	padding: 0rpx 30rpx;
}
.ntop {
	margin-top: 20rpx;
	padding: 20rpx 30rpx;
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #808080;
	line-height: 28rpx;
}
.switchBox {
	background-color: #fff;
	.switchView {
		display: flex;
		justify-content: space-between;
		align-items: center;
		align-items: center;
		padding: 25rpx 10rpx;
		text {
			font-size: 32rpx;
			font-weight: 400;
			color: #1a1a1a;
			line-height: 32rpx;
		}
	}
}
</style>
